#include "registerwidget.h"
#include "model/data.h"
#include "toast.h"

#include <QGridLayout>
#include <QHBoxLayout>


RegisterWidget::RegisterWidget(QWidget *parent): QDialog{parent}
{
    // 1. 设置本窗口的基本属性
    this->setFixedSize(420, 320);
    this->setWindowTitle("注册");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 15, 50, 10);
    this->setLayout(layout);

    // 3. 创建标题区域（图标 + 文字）
    QWidget* titleWidget = new QWidget();
    titleWidget->setFixedHeight(50);
    QHBoxLayout* titleLayout = new QHBoxLayout(titleWidget);
    titleLayout->setContentsMargins(0, 0, 0, 5);

    // 创建图标 QLabel
    QLabel* iconLabel = new QLabel();
    QPixmap iconPixmap(":/resource/image/logo.png");
    iconPixmap = iconPixmap.scaled(42, 42, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    iconLabel->setPixmap(iconPixmap);
    iconLabel->setFixedSize(42, 42);

    // 创建文字标题
    titleLabel = new QLabel();
    titleLabel->setText("注册");
    titleLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    titleLabel->setStyleSheet("QLabel { font-size: 32px; font-weight: 600; }");

    titleLayout->addStretch(1);
    titleLayout->addWidget(iconLabel);
    titleLayout->addSpacing(10);
    titleLayout->addWidget(titleLabel);
    titleLayout->addStretch(1);

    // 4. 创建输入框
    usernameEdit = model::createLineEdit(QIcon(":/resource/image/username.png"), "输入用户名");
    passwordEdit = model::createLineEdit(QIcon(":/resource/image/password.png"), "输入密码");
    passwordEdit->setEchoMode(QLineEdit::Password); // 密码输入
    phoneEdit = model::createLineEdit(QIcon(":/resource/image/phone.png"), "请输入手机号");
    verifyCodeEdit = model::createLineEdit(QIcon(":/resource/image/mes_verifycode.png"), "请输入验证码");

    sendVerifyCodeBtn = new QPushButton();
    sendVerifyCodeBtn->setFixedSize(100, 40);
    sendVerifyCodeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    sendVerifyCodeBtn->setText("发送验证码");
    sendVerifyCodeBtn->setStyleSheet(R"(
        QPushButton { border: none; border-radius: 10px; background-color: transparent; }
        QPushButton:pressed { background-color: rgb(240, 240, 240); }
    )");


    // 5. 创建注册按钮
    submitBtn = new QPushButton();
    submitBtn->setText("注册");
    submitBtn->setFixedHeight(40);
    submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    submitBtn->setStyleSheet(R"(
        QPushButton {
            border: none;
            border-radius: 12px;
            background-color: rgb(7, 193, 96);
            color: rgb(255, 255, 255);
        }
        QPushButton:pressed {
            background-color: rgb(6, 174, 86);
        }
    )");


    // 6. 添加到布局管理器中
    layout->addWidget(titleWidget, 0, 0, 1, 5);
    layout->addWidget(usernameEdit, 1, 0, 1, 5);
    layout->addWidget(passwordEdit, 2, 0, 1, 5);
    layout->addWidget(phoneEdit, 3, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 4, 0, 1, 4);
    layout->addWidget(sendVerifyCodeBtn, 4, 4, 1, 1);
    layout->addWidget(submitBtn, 5, 0, 1, 5);

    // 7. 处理信号槽
    connect(submitBtn, &QPushButton::clicked, this, &RegisterWidget::clickSubmitBtn);
}

void RegisterWidget::clickSubmitBtn()
{
    // // 1. 先从输入框拿到必要的内容
    // const QString& username = usernameEdit->text();
    // const QString& password = passwordEdit->text();
    // const QString& verifyCode = verifyCodeEdit->text();
    // if (username.isEmpty() || password.isEmpty() || verifyCode.isEmpty()) {
    //     Toast::showMessage("用户名/密码/验证码不能为空!");
    //     return;
    // }

    // // 2. 对比验证码是否正确
    // if (!verifyCodeWidget->checkVerifyCode(verifyCode)) {
    //     Toast::showMessage("验证码不正确!");
    //     return;
    // }

    Toast::showMessage("注册成功, 请重新登录");

    this->close();
}
